package com.zed.games.answer;

import com.zed.api.common.param.ParamsBuilder;
import com.zed.api.games.ActionReply;
import com.zed.api.games.DefaultActionReply;
import com.zed.api.games.Game;
import com.zed.api.games.GameException;
import com.zed.api.games.answer.AnswerGameLevel;
import com.zed.api.games.answer.action.AnswerAction;
import com.zed.api.games.answer.action.ChooseAnswerAction;
import com.zed.api.games.answer.action.GetTipsAction;
import com.zed.api.games.answer.show.AnswerShowWorld;
import com.zed.api.games.answer.show.CorrectAlternativeAnswer;

/**
 * 统一答题指令处理器
 *
 * @author Zed
 */
class UnifyAnswerActionProcessor {

    private static UnifyAnswerActionProcessor PROCESSOR = new UnifyAnswerActionProcessor();

    /**
     * 对{@link AnswerAction}得指令回答
     *
     * @param gameLevel {@link AnswerGameLevel}, 关卡
     * @param action    {@link AnswerAction}, 动作
     * @return {@link ActionReply},响应
     */
    ActionReply processor(AnswerGameLevel gameLevel, AnswerAction action) throws GameException {
        //TODO 对答案得响应
        Game game = gameLevel.getGame();
        AnswerShowWorld showWorld = gameLevel.getShowWorld();
        //选择答案的回复
        if (action instanceof ChooseAnswerAction) {
            CorrectAlternativeAnswer correctAlternativeAnswer = showWorld.getCorrectAlternativeAnswer();
            String chooseAnswer = ((ChooseAnswerAction) action).getChooseAnswer();
            if (correctAlternativeAnswer.isBaseOnIdCompareAnswers(chooseAnswer)) {
                return null;
            }
            throw new GameException("选择的答案错误。");
        }

        if (action instanceof GetTipsAction) {
            String tips = showWorld.getOneTips();
            if (tips == null) {
                throw new GameException("该关卡的提示都用完啦");
            }
            ParamsBuilder paramsBuilder = new ParamsBuilder(1);
            paramsBuilder.setParameter(action.getIdentifier(), tips);
            return new DefaultActionReply(game, gameLevel, paramsBuilder);
        }


        return null;
    }

    /**
     * 获取单列
     *
     * @return {@link UnifyAnswerActionProcessor}
     */
    public static UnifyAnswerActionProcessor getInstance() {
        return PROCESSOR;
    }
}
